# Copyright (c) 2022, hzmes and contributors
# For license information, please see license.txt


import frappe
from frappe import _


def execute(filters=None):
	# get_data要传参filters
	# message = []
	# message.append("<br>The value of the Creation To Filter is : " + filters.get("creation_to"))
	return get_columns(filters), get_data(filters)

def get_columns(filters):
	columns = [
		{
			"label": _("状态"),
			"fieldtype": "Data",
			"fieldname": "workflow_state",
			"width": 120,
		},
		{
			"label": _("Follow Status"),
			"fieldtype": "Data",
			"fieldname": "follow_status",
			"width": 110,
		},
		{
			"label": _("物料名称"),
			"fieldtype": "Data",
			"fieldname": "sample_category_purchase",
			"width": 100,
		},
		{
			"label": _("规格型号"),
			"fieldtype": "Data",
			"fieldname": "sample_model",
			"width": 280,
		},
		{
			"label": _("检测编号"),
			"fieldtype": "Link",
			"options": "SRM Sample Admit",
			"fieldname": "name",
			"width": 120,
		},
		{
			"label": _("申请单号"),
			"fieldtype": "Data",
			"fieldname": "rcbillno",
			"width": 150,
		},
		{
			"label": _("要求完成日期"),
			"fieldtype": "Date",
			"fieldname": "requirement_date",
			"width": 120,
		},
		{
			"label": _("供应商"),
			"fieldtype": "Data",
			"fieldname": "supplier_name",
			"width": 150,
		},
		{
			"label": _("供应商性质"),
			"fieldtype": "Data",
			"fieldname": "company_nature",
			"width": 100,
		},
		{
			"label": _("指派人"),
			"fieldtype": "Data",
			"fieldname": "assignor",
			"width": 100,
		},
		{
			"label": _("送测人"),
			"fieldtype": "Data",
			"fieldname": "submission_user_username",
			"width": 70,
		},
		{
			"label": _("项目名称"),
			"fieldtype": "Data",
			"fieldname": "project_name",
			"width": 100,
		},
		{
			"label": _("ERP编码"),
			"fieldtype": "Data",
			"fieldname": "material_number",
			"width": 120,
		},
		{
			"label": _("规格书"),
			"fieldtype": "Data",
			"fieldname": "specification",
			"width": 80,
		},
		{
			"label": _("测试报告"),
			"fieldtype": "Data",
			"fieldname": "detection_result",
			"width": 80,
		},
		{
			"label": _("图纸"),
			"fieldtype": "Data",
			"fieldname": "drawing",
			"width": 60,
		},
		{
			"label": _("Source File"),
			"fieldtype": "Data",
			"fieldname": "source_file",
			"width": 80,
		},
		{
			"label": _("补充附件"),
			"fieldtype": "Data",
			"fieldname": "add_attachment",
			"width": 100,
		},
		{
			"label": _("SRM Sample Admit"),
			"fieldtype": "Data",
			"fieldname": "print_pdf",
			"width": 100,
		},
		{
			"label": _("承认日期"),
			"fieldtype": "Date",
			"fieldname": "approva_finish_date",
			"width": 120,
		},
		{
			"label": _("创建日期"),
			"fieldtype": "Date",
			"fieldname": "creation",
			"width": 120,
		},
		{
			"label": _("创建人"),
			"fieldtype": "Data",
			"fieldname": "submission_user_username",
			"width": 70,
		},
		{
			"label": _("是否下放"),
			"fieldtype": "Data",
			"fieldname": "is_assign",
			"width": 90,
		},
		{
			"label": _("编码审核日期"),
			"fieldtype": "Date",
			"fieldname": "examines_date",
			"width": 120,
		},
	]

	return columns


def get_data(filters):
	# 获取filters
	# frappe.msgprint(f'the filter is {filters}')
	# 2023-04-07: 增加follow_status这个过滤条件
	workflow_state, creation_from, creation_to, follow_status = filters.get('workflow_state'), filters.get('creation_from'), filters.get('creation_to'), filters.get('follow_status')
	# 先配置默认conditions再通过if判断是否增加条件项
	conditions = " AND 1=1 "
	if workflow_state:
		conditions += f" AND s1.workflow_state = '{workflow_state}' "
	if follow_status:
		conditions += f" AND s1.follow_status = '{follow_status}' "
	# 把conditions组合进来
	dt = frappe.db.sql(
		f"""select concat('<a href=', s1.print_pdf, '>', '下载', '</a>') as print_pdf, 
		concat('<a href=', s1.specification, '>', '下载', '</a>') as specification, 
		concat('<a href=', s1.detection_result, '>', '下载', '</a>') as detection_result, 
		concat('<a href=', s1.drawing, '>', '下载', '</a>') as drawing, 
		concat('<a href=', s1.source_file, '>', '下载', '</a>') as source_file,
		concat('<a href=', '', '>', '下载', '</a>') as add_attachment,
		s1.workflow_state, s1.is_assign, s1.quality_confirm_user, s1.name, s1.sample_category_purchase, 
		s1.sample_model, s1.material_number, s1.company_nature, s1.supplier_name, s1.approva_finish_date, 
		s1.creation, s1.submission_user_username,
		s1.rcbillno,
		s1.requirement_date,
		s1.assignor,
		IFNULL(s1.follow_status, '') as follow_status,
		s1.project_name,
		s1.submission_user_username,
		s1.examines_date,
		case s1.is_assign when 0 then '未下发' else '已下发' end as is_assign
		FROM `tabSRM Sample Admit` s1
		WHERE s1.creation >= '{creation_from}' and s1.creation <= '{creation_to}' {conditions}
		ORDER BY s1.creation DESC;
		""", as_dict=True
	)

	return dt
